#include "camera.h"
#include "CMemoryFile.h"
#include "sceneobject/sceneobjectbase.h"
#include "freshpoint.h"

using namespace io;

namespace fireplane
{
	Camera::Camera():
		_enable_move(true)
	{
		_s_Camera = this;
	}

	Camera::~Camera()
	{
		_s_Camera = NULL;
	}

	void Camera::init()
	{
		_all_map_node = CCNode::create();
		addChild(_all_map_node);

		_all_backgroud = CCNode::create();
		_all_map_node->addChild(_all_backgroud);

		_all_scene_object = CCNode::create();
		_all_map_node->addChild(_all_scene_object);

		_all_bullet_parent = CCNode::create();
		addChild(_all_bullet_parent);

		_hero_node = CCNode::create();
		addChild(_hero_node);

		_all_enemy_parent = CCNode::create();
		addChild(_all_enemy_parent);

		_all_effect_node = CCNode::create();
		addChild(_all_effect_node);

		scheduleUpdate();
	}

	const char* _all_map_level[] = 
	{
		"mapdata/level0/prjOut/L1.out"
	};

	const char* _all_map_level_bg[] = 
	{
		"mapdata/level0/bgjpg/"
	};

	const char* _all_map_level_sceneobj[] = 
	{
		"mapdata/level0/sceneobject/"
	};

	void Camera::_loadSceneObject(CMemoryFile* file,int level)
	{
		int size;
		file->read((char*)&size,sizeof(int));

		for (int i = 0;i < size;i++)
		{
			char buffer[0xff];
			int s;
			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;


			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;
		}

		file->read((char*)&size,sizeof(int));

		for (int i = 0;i < size;i++)
		{
			char buffer[0xff];
			int s;
			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;

			bool must = false;
			if(string(buffer).compare("1_ZJ_5_3:71") == 0){
				int xxx = 100;
				xxx = 200;

				must = true;
			}

			
			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;

			float x;
			file->read((char*)&x,sizeof(float));

			float y;
			file->read((char*)&y,sizeof(float));

			int z;
			file->read((char*)&z,sizeof(int));

			

			float angle;
			file->read((char*)&angle,sizeof(float));

			float x_s;
			file->read((char*)&x_s,sizeof(float));

			float y_s;
			file->read((char*)&y_s,sizeof(float));

			if(fabs(angle) == 180.0f){
				x_s = -x_s;
				y_s = -y_s;
				angle = 0;
			}

			CCNode* parent = NULL;
			string fileName(buffer);
		
			parent = _all_scene_object;
 			createSceneObject(buffer,
 				x,y,z,angle,x_s,y_s,level,
				parent);
		}
	}

	void Camera::_loadBg(CMemoryFile* file,int level)
	{
		int s;
		file->read((char*)&s,sizeof(int));

		float y = 0;
		_all_backgroud->removeAllChildrenWithCleanup(true);

		for (int i = 0;i < s;i++)
		{
			char buffer[0xff];
			int s;
			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;

			file->read((char*)&s,sizeof(int));
			file->read((char*)buffer,s);
			buffer[s] = 0;

			string fileName = _all_map_level_bg[level];
			fileName.append(buffer);

			BgObject* ret = BgObject::create(fileName,ccp(0,y));
			_all_backgroud->addChild(ret);
			y += ret->getContentSize().height;
		}
	}

	void Camera::loadMap( int level )
	{
		unsigned long size;
		unsigned char* buffer = CCFileUtils::sharedFileUtils()->getFileData(_all_map_level[level],"rb",&size);

		CMemoryFile* file = createMemoryReadFile(buffer,size,_all_map_level[level],true);
		_loadBg(file,level);
		_loadSceneObject(file,level);
	}

	void Camera::createSceneObject( string buffer, float x,float y,int z,
		float angle,float x_s,float y_s,int level,CCNode* parentNode)
	{
		string f(_all_map_level_sceneobj[level]);
		f.append(buffer);

		CCNode* sceneobject = 
			sceneobject = SceneObjectBase::create(f,ccp(x,y),angle,x_s,y_s);

		parentNode->addChild(sceneobject,z);
	}

	void Camera::update( float dt )
	{
		if(!_enable_move)
			return;

		static float speed = 30.0f;
		float movey = speed * dt;

		CCPoint pos = _all_map_node->getPosition();
		pos.y -= movey;
		_all_map_node->setPosition(pos);

		FreshPointBase::UpdateAllFreshPoint(dt);
	}

	Camera* Camera::_s_Camera = NULL;

}